package com.woniuxy.reader.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.reader.entity.Collection;
import com.woniuxy.reader.service.CollectionService;
import com.woniuxy.result.ResponseResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh_o
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {

    @Resource
    private CollectionService collectionService;

    /**
     * 根据用户id得到该用户收藏书的集合
     */
    @GetMapping("{userId}")
    @ApiOperation("根据用户id得到该用户收藏书的信息集合")
    @ApiImplicitParam(name="id",value="用户id")
    public Object getBooks(@PathVariable("userId") Integer userId)throws Exception{
        return collectionService.getBooks(userId);
    }

    /**
     * 根据收藏id，用户id  删除用户收藏的书籍
     */
    @DeleteMapping
    public Object deleteBook(Integer bid,Integer uid)throws Exception{
        return collectionService.deleteBook(bid, uid);
    }

    /**
     * 新增收藏
     * @param bookId 书籍id
     * @param userId 用户id
     */
    @PostMapping("{bookId}/{userID}")
    public Object insert(@PathVariable("bookId") Integer bookId, @PathVariable("userID") Integer userId) {
        collectionService.save(new Collection().setBookId(bookId).setUserId(userId));
        return ResponseResult.success();
    }

    /**
     * 取消收藏
     * @param bookId 书籍id
     * @param userId 用户id
     */
    @DeleteMapping("{bookId}/{userID}")
    public Object remove(@PathVariable("bookId") Integer bookId, @PathVariable("userID") Integer userId) {
        collectionService.remove(new QueryWrapper<Collection>().eq(Collection.BOOK_ID, bookId).eq(Collection.USER_ID, userId));
        return ResponseResult.success();
    }

    /**
     * 判断用户是否已收藏此书籍
     * @param bookId 书籍id
     * @param userId 用户id
     */
    @GetMapping("{bookId}/{userID}")
    public boolean isCollected(@PathVariable("bookId") Integer bookId, @PathVariable("userID") Integer userId) {
        return collectionService.getOne(new QueryWrapper<Collection>().eq(Collection.BOOK_ID, bookId).eq(Collection.USER_ID, userId)) != null;
    }
}

